package com.easonji.attendance.service.impl;

import com.easonji.attendance.model.Procedure;
import com.easonji.attendance.mapper.ProcedureMapper;
import com.easonji.attendance.service.IProcedureService;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.page.PageInfo;
import com.easonji.common.util.StringUtils;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.factory.PageFactory;

import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import com.alibaba.fastjson.JSONObject;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 流程管理 服务实现类
 * </p>
 *
 * @author easonji
 * @since 2018-10-01
 */
@Service
public class ProcedureServiceImpl extends AbstractBaseService<Procedure> implements IProcedureService {

    @Resource
    private ProcedureMapper procedureMapper;

    @Override
    protected JSONObject warp2Json(Procedure procedure) {
       JSONObject result = JsonUtils.parseObject(procedure);

       return result;
    }


    @Override
    public Object queryList(BaseQuery baseQuery){

        // 分页信息
        Page<Procedure> page = new PageFactory<Procedure>().defaultPage();
        // 查询条件
        Wrapper<Procedure> wrapper = new EntityWrapper<>();

        List<Procedure> procedures = procedureMapper.selectPage(page, wrapper);

        return new PageInfo(warp(procedures), page.getTotal());
    }

    @Override
    public Object queryRowById(Integer proId) {
        Procedure procedure = procedureMapper.selectById(proId);
        return warp(procedure);
    }

    @Override
    public Integer save(Procedure procedure) {

        return procedureMapper.insert(procedure);
    }

    @Override
    public Integer update(Procedure procedureForm) {
        Procedure procedure = new Procedure();
        BeanUtils.copyProperties(procedureForm,procedure);
        return procedureMapper.updateById(procedure);
    }

    @Override
    public Integer delete(Integer proId) {
        Wrapper<Procedure> wrapper = new EntityWrapper<>();
        wrapper.eq("pro_id", proId);
        return procedureMapper.delete(wrapper);
    }

}
